package com.foreverwin.mes.so.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.so.service.PlannedOrderService;
import com.foreverwin.mes.so.model.PlannedOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Ervin Chen
 * @since 2020-03-26
 */
@Controller
@RequestMapping("/plannedOrders")
public class PlannedOrderController {
    private final Logger logger = LoggerFactory.getLogger(PlannedOrderController.class);

    @Autowired
    public PlannedOrderService plannedOrderService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getPlannedOrderById(@PathVariable String id) {
        PlannedOrder result;
        try {
            result = plannedOrderService.selectById(id);
        } catch (Exception e) {
            logger.error("getPlannedOrderById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getPlannedOrderList(PlannedOrder plannedOrder){
        List<PlannedOrder> result;
        try {
            EntityWrapper<PlannedOrder> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(plannedOrder);
            result = plannedOrderService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getPlannedOrderList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getPlannedOrderPageList")
    public AjaxResult getPlannedOrderPageList(FrontPage<PlannedOrder> frontPage, PlannedOrder plannedOrder){
        Page result;
        try {
            String site = CommonMethods.getSite();
            plannedOrder.setSite(site);
            EntityWrapper<PlannedOrder> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(plannedOrder);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("HANDLE", frontPage.getGlobalQuery())
                    .or().like("SITE", frontPage.getGlobalQuery())
                    .or().like("PLANNED_ORDER", frontPage.getGlobalQuery())
                    .or().like("QUANTITY_IS_FIXED", frontPage.getGlobalQuery())
                    .or().like("ITEM", frontPage.getGlobalQuery())
                    .or().like("UNIT_OF_MEASURE", frontPage.getGlobalQuery())
                    .or().like("COST_COLLECTOR", frontPage.getGlobalQuery())
        ;
            }
            result = plannedOrderService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getPlannedOrderPageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param plannedOrder  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult plannedOrderSave(@RequestBody PlannedOrder plannedOrder) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = plannedOrderService.insertOrUpdate(plannedOrder) ? 1 : 0;
        } catch (Exception e) {
            logger.error("plannedOrderSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : plannedOrder);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult plannedOrderDelete(@PathVariable("id") String id){
        int count;
        try {
            count = plannedOrderService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("plannedOrderDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = plannedOrderService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("plannedOrderBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}